<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 下午5:44
 */

class ModLevel {

    public static function indexData($params){
        $table = 'tblGameDAULevel';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end
                        AND Platform=:platform AND GameSrc=:game ';
        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':game'     => $params['g'],
            ':platform' => $params['os'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL' && $params['server'] !== 'LIST'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else {
            $where .= 'AND SID = "ALL"';
        }

        $sql = "SELECT * FROM {$table} {$where} order by RecDate desc, SID asc";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }

    public static function roleData($params){
        $table = 'tblGameRoleRocLevel';

        $where  = ' WHERE RecDate = :date AND Period=:period AND Platform=:platform AND GameSrc=:game ';
        $args   = array(
            ':date'     => $params['time'],
            ':period'   => "Daily",
            ':game'     => $params['g'],
            ':platform' => $params['os'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL'){
            $where .= ' AND SID=:server ';
            $args[':server'] = $params['server'];
        }else {
            $where .= ' AND SID = "ALL" ';
        }

        if(!empty($params['min_level'])){
            $where .= ' AND Level>=:min_level ';
            $args[':min_level'] = intval($params['min_level']);
        }

        if(!empty($params['max_level'])){
            $where .= ' AND Level<=:max_level ';
            $args[':max_level'] = intval($params['max_level']);
        }

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }

    public static function detailsData($params){
        $table = 'tblGameDAULevel_Details';

        $where  = ' WHERE RecDate = :date AND Period=:period AND Platform=:platform AND GameSrc=:game ';
        $args   = array(
            ':date'     => $params['time'],
            ':period'   => "Daily",
            ':game'     => $params['g'],
            ':platform' => $params['os'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL'){
            $where .= ' AND SID=:server ';
            $args[':server'] = $params['server'];
        }else {
            $where .= ' AND SID = "ALL" ';
        }

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }

    public static function independentData($params){
        $table = 'tblGameLevelLost';
        $where  = ' WHERE RecDate BETWEEN :begin AND :end
                        AND Platform=:platform AND GameSrc=:game AND KPI =:kpi ';
        $args   = array(
            ':begin'    => $params['time_start'],
            ':end'      => $params['time_end'],
            ':period'   => $params['period'],
            ':platform' => $params['p'],
            ':game'     => $params['g'],
            ':kpi'      => $params['kpi'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL' && $params['server'] !== 'LIST'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else if($params['server'] == 'LIST'){
            $where .= 'AND SID in (:server)';
            $args[':server'] = PubBase::getAllServer($params['p'], $params['g']);
        }else{
            $where .= 'AND SID = "ALL"';
        }

        $sql = "SELECT RecDate,SID,RoleUser,RoleLevel
                FROM {$table} {$where} order by RecDate desc, RoleLevel asc,SID asc";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }


    public static function distributeData($params){
        $table = 'tblGameLevelLost';
        $where  = ' WHERE RecDate = :date AND Platform= :platform AND GameSrc=:game AND KPI = :kpi ';
        $args   = array(
            ':date'         => $params['date'],
            ':platform'     => $params['p'],
            ':game'         => $params['g'],
            ':kpi'          => $params['kpi'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL' && $params['server'] !== 'LIST'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else if($params['server'] == 'LIST'){
            $where .= 'AND SID in (:server)';
            $args[':server'] = PubBase::getAllServer($params['p'], $params['g']);
        }else{
            $where .= 'AND SID = "ALL"';
        }

        $sql = "SELECT RecDate,SID,RoleLevel,RoleUser,KPI,LostDay1,LostDay2,LostDay3,LostDay4,LostDay5,LostDay6,LostDay7
                FROM {$table} {$where} order by RecDate desc,RoleLevel asc, SID asc";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }

    public static function independentKPIData($params){
        $table = 'tblGameLevelLost';
        $where  = ' WHERE RecDate = :date AND Platform=:platform AND GameSrc=:game  ';
        $args   = array(
            ':date'     => $params['date'],
            ':period'   => $params['period'],
            ':game'     => $params['g'],
            ':platform' => $params['p']
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL' && $params['server'] !== 'LIST'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else if($params['server'] == 'LIST'){
            $where .= 'AND SID in (:server)';
            $args[':server'] = PubBase::getAllServer($params['p'], $params['g']);
        }else{
            $where .= 'AND SID = "ALL"';
        }

        $sql = "SELECT RecDate,SID,KPI,RoleUser,RoleLevel
                FROM {$table} {$where} order by RecDate desc, RoleLevel asc,SID asc";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        return $dbLink->query($sql, $args);
    }

    public static function activeData($params){
        $table = 'tblGameLevelActivity';
        $where  = ' WHERE RecDate = :time
                        AND Period=:period
                        AND Platform=:platform 
                        AND GameSrc=:game 
                        AND SID=:server ';

        $args   = array(
            ':time'     => $params['time'],
            ':period'   => 'Daily',
            ':platform' => $params['os'],
            ':game'     => $params['g'],
            ':server'   => empty($params['server']) ? 'ALL' : $params['server'],
        );

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));

        return $dbLink->query($sql, $args);
    }
} 
